package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.domain.Server;
import com.dss.auto_deploy.dto.*;
import com.dss.auto_deploy.service.IServerService;
import com.dss.auto_deploy.util.FileBean;
import com.dss.auto_deploy.util.RemoteSshUtil;
import com.dss.auto_deploy.util.Result;
import com.dss.auto_deploy.util.StringUtils;
import com.dss.auto_deploy.vo.FolderVo;
import com.dss.auto_deploy.vo.ServerVo;
import com.dss.auto_deploy.vo.SftpDownloadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
* 虚拟机服务器
* @Description:虚拟机服务器控制层
* @author dongshoushan
* @date 2022-09-06 15:29:28
*/
@RestController
@RequestMapping("/server")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: 虚拟机服务器")
public class ServerController {

    /**
    * 虚拟机服务器Service
    */
    private final IServerService serverService;
    private final RemoteSshUtil remoteSshUtil;
    private final HttpServletResponse response;
    private final HttpServletRequest request;
    /**
    * 新增虚拟机服务器
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增虚拟机服务器", notes="新增虚拟机服务器")
    public Result insert(@RequestBody @Valid ServerAddDto addDto) {
        serverService.insert(addDto);
        return Result.success();
    }

    /**
    * 修改虚拟机服务器
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改虚拟机服务器", notes="修改虚拟机服务器")
    public Result update(@RequestBody @Valid ServerUpdateDto updateDto) {
        serverService.update(updateDto);
        return Result.success();
    }

    /**
    * 批量删除虚拟机服务器
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除虚拟机服务器", notes="删除虚拟机服务器")
    public Result deleteBatch(@PathVariable Long[] ids) {
        serverService.deleteBatch(ids);
        return Result.success();
    }

    /**
    * 查询虚拟机服务器
    * @return
    */
    @GetMapping("/list")
    @ApiOperation(value = "查询虚拟机服务器", notes="查询虚拟机服务器")
    public List<ServerVo> list(){
        return serverService.getList();
    }
    @GetMapping("/updateLinuxInfo")
    @ApiOperation(value = "更新linux信息", notes="更新linux信息")
    public Result updateLinuxInfo(){
        serverService.updateLinuxInfo();
        return Result.success();
    }
    /**
    * 查询目录
    * @param
    * @return
    */
    @GetMapping("/getAllFolder")
    @ApiOperation(value = "查询目录", notes="查询目录")
    public List<FolderVo> getAllFolder(){
        return serverService.getAllFolder();
    }

    /**
    * 查询虚拟机
    * @param
    * @return
    */
    @GetMapping("/getAllServer")
    @ApiOperation(value = "查询虚拟机", notes="查询虚拟机")
    public List<ServerVo> getAllServer(String ip){
        return serverService.getAllServer(ip);
    }
    @GetMapping("/getServerByIp")
    @ApiOperation(value = "根据ip查询虚拟机", notes="根据ip查询虚拟机")
    public Result getServerByIp(String ip){
        return Result.success(serverService.getServerByIp(ip));
    }
    @GetMapping("/getServerById/{id}")
    @ApiOperation(value = "根据id查询虚拟机", notes="根据id查询虚拟机")
    public Result getServerById(@PathVariable Long id){
        return Result.success(serverService.getServerById(id));
    }
    @GetMapping("/getLoclIp")
    @ApiOperation(value = "获取本机ip", notes="获取本机ip")
    public Result getLoclIp(){
        return Result.success(serverService.getLoclIp());
    }


    @GetMapping("queryFileByPath")
    @ApiOperation(value = "获取文件列表", notes="获取文件列表")
    public Result<List<FileBean>> queryFileByPath(QueryFileByPathDto dto) {
        Server server = serverService.getServerByIp(dto.getIp());
        List<FileBean> listFiles = remoteSshUtil.listFiles(dto.getPath(), server);
        if (StringUtils.isNotEmpty(dto.getName())){
            listFiles = listFiles.stream().filter(fileBean -> fileBean.getFileName().contains(dto.getName())).collect(Collectors.toList());
        }
        return Result.success(listFiles);
    }
    @PostMapping("downloadForTemp")
    @ApiOperation(value = "编辑文件临时下载", notes="编辑文件临时下载")
    public Result<SftpDownloadVo> downloadForTemp(@RequestBody QueryFileByPathDto dto) throws Exception {
        SftpDownloadVo sftpDownloadVo = serverService.downloadForTemp(dto);
        return Result.success(sftpDownloadVo);
    }
    @PostMapping("addServerFile")
    @ApiOperation(value = "新增文件", notes="新增文件")
    public Result addServerFile(@RequestBody ServerFileAddDto dto) {
        serverService.addServerFile(dto);
        return Result.success();
    }
    @PostMapping("checkPathIsTrue")
    @ApiOperation(value = "校验路径是否存在", notes="校验路径是否存在")
    public Result checkPathIsTrue(@RequestBody CheckPathIsTrueDto dto) {
        if ( serverService.checkPathIsTrue(dto)){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @PostMapping("delServerFile")
    @ApiOperation(value = "删除文件", notes="删除文件")
    public Result delServerFile(@RequestBody List<ServerFileDelDto>  list) {
        serverService.delServerFile(list);
        return Result.success();
    }
    @PutMapping("reNameServerFile")
    @ApiOperation(value = "重命名文件", notes="重命名文件")
    public Result reNameServerFile(@RequestBody ServerFileReNameDto  dto) {
        serverService.reNameServerFile(dto);
        return Result.success();
    }
    @GetMapping("download")
    @ApiOperation(value = "sftp下载", notes="sftp下载")
    public void download(QueryFileByPathDto dto) throws Exception {
        serverService.download(dto,response);
    }
    @GetMapping("downloadList")
    @ApiOperation(value = "sftp批量下载", notes="sftp批量下载")
    public void downloadList(QueryFileByPathDto dto) throws Exception {
        serverService.downloadList(dto,response);
    }
    @PostMapping("upload")
    @ApiOperation(value = "上传文件", notes="上传文件")
    public Result upload(@RequestParam(value = "ip") String ip,
                    @RequestParam(value = "path") String path,
                    @RequestPart(value = "file") MultipartFile file) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        serverService.upload(ip,path,file);
        return Result.success();
    }
    @PutMapping("saveContentForTemp")
    @ApiOperation(value = "编辑文件后进行保存", notes="编辑文件后进行保存")
    public Result saveContentForTemp(@RequestBody SftpDownloadVo dto) throws Exception {
        serverService.saveContentForTemp(dto);
        return Result.success();
    }
}
